﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using bzxxxt.admin.entity;
using bzxxxt.admin.service;
using bzxxxt.bll;
namespace bzxxxt
{
    public partial class aa : System.Web.UI.Page
    {
        private string connstr = ConfigurationManager.ConnectionStrings["newconn"].ConnectionString;
        private string oldstr = ConfigurationManager.ConnectionStrings["olddbconn"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            UserEntity user = UserService.GetUser(HttpContext.Current);
            if (user == null)
            {
                Response.Write(JsonUtil.ToJson(new { success = false, content = "无权访问！" }));
                Response.End();
                return;
            }
            if (cbDB.Checked) connstr = oldstr;
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {            
            try
            {
                string sql = txtContent.Text.Trim();
                if (!cbQuery.Checked)
                {
                    SqlConnection conn = new SqlConnection(connstr);
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.CommandTimeout = Cfg.TimeOut;
                    int c = cmd.ExecuteNonQuery();
                    lblMsg.Text = "" + c;
                }
                else
                {
                    dsSQL.SelectCommand = sql;
                    dsSQL.ConnectionString = connstr;
                    dsSQL.DataBind();

                }
            }
            catch (Exception ex) {
                lblMsg.Text = ex.Message;
            }
        }

        protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvData.PageIndex = e.NewPageIndex;
            string sql = txtContent.Text.Trim();
            
            if (sql.StartsWith("select"))           
            {
                dsSQL.SelectCommand = sql;
                dsSQL.ConnectionString = connstr;
                dsSQL.DataBind();
            }
        }
    }
}